package wsz.sharding.algorithm;

import com.google.common.collect.Range;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Collection;
import java.util.LinkedHashSet;

/**
 * TODO：
 *
 * @author wsz
 * @desc：
 * @date 2022/6/7
 */
public class YearRangeAlgorithm implements RangeShardingAlgorithm<Integer> {
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Integer> shardingValue) {
        Collection<String> result = new LinkedHashSet<>();
        //逻辑表名
        String logicTableName = shardingValue.getLogicTableName();
        Range<Integer> shardingKey = shardingValue.getValueRange();
        //范围查询最小值
        Integer start = shardingKey.lowerEndpoint();
        //范围查询最大值
        Integer end = shardingKey.upperEndpoint();

        // FIXME 先简单处理
        result.add(logicTableName+"_"+start);
        result.add(logicTableName+"_"+end);
        return result;
    }
}
